I realize that in order to set a global hook the process must have access to the user's desktop. And if a process is running as a Windows service (under the SYSTEM account), it doesn't have access to the desktop.
My code works perfectly as a user-mode process, but I'm trying to get it to run the same as a Windows service. I have tried to manually gain access to the desktop using this function:
Code:
void ConnectToDesktop ()
{
HWINSTA hWS;
HDESK hDT;
// Connect to the window station
hWS = OpenWindowStation ( "Winsta0", FALSE, MAXIMUM_ALLOWED);
if(!SetProcessWindowStation (hWS))
printf("SetProcessWindowStation()\n");
//Connect to the desktop
hDT = OpenDesktop ( "Default", 0, FALSE, MAXIMUM_ALLOWED);
SetThreadDesktop (hDT);
}
int main(int argc, char** argv)
{
ConnectToDesktop();
// SetWindowsHookEx() etc... (WH_KEYBOARD_LL and WH_MOUSE_LL)
return 0;
}
I got this function from this article: Override Windows Creation Parameters with a Win32 Service - CodeProject®
This may have worked on WinXP (although I have not tested it), however it doesn't seem to work on Windows 7.
Am I just out of luck because it's Windows 7?
I have also used CreateProcessAsUser() to run it as a normal user (from the service process), and the hook still does not function.